package com.leoman.service.impl;

import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.dao.AppUserDao;
import com.leoman.dao.CompanyUserDao;
import com.leoman.dao.FeedBackDao;
import com.leoman.entity.FeedBack;
import com.leoman.service.FeedBackService;
import com.leoman.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * FeedBackServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:22.
 */
@Service
public class FeedBackServiceImpl implements FeedBackService {

    @Autowired
    private FeedBackDao feedBackDao;

    @Autowired
    private AppUserDao appUserDao;

    @Autowired
    private CompanyUserDao companyUserDao;

    @Override
    public List<FeedBack> findAll() {
        return feedBackDao.findAll();
    }

    @Override
    public Page<FeedBack> find(int pageNum, int pageSize) {
        return feedBackDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<FeedBack> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public FeedBack getById(int id) {
        return feedBackDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        feedBackDao.delete(id);
    }

    @Override
    public FeedBack create(FeedBack feedBack) {
        return feedBackDao.save(feedBack);
    }

    @Override
    public FeedBack update(FeedBack feedBack) {
        return feedBackDao.save(feedBack);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Integer findCountWithNewAdd(Integer index) {
        return feedBackDao.findListByCreateTime(CommonController.getFirstTime(index)).size();
    }

    @Override
    public Page<FeedBack> pageByParams(String nickName, String mobile, Integer sendType, Integer pageNum, Integer pageSize) {
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);
        Pageable pageable = new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "id");

        // 处理数据
        if (StringUtils.isNotBlank(nickName)) {
            nickName = "%" + nickName + "%";
        }
        if (StringUtils.isNotBlank(mobile)) {
            mobile = "%" + mobile + "%";
        }

        Page<FeedBack> page;

        if (StringUtils.isNotBlank(nickName)) {
            if (StringUtils.isNotBlank(mobile)) {
                if (null != sendType) {
                    page = feedBackDao.pageWithNickNameAndMobileAndSendType(nickName, mobile, sendType, pageable);
                } else {
                    page = feedBackDao.pageWithNickNameAndMobile(nickName, mobile, pageable);
                }
            } else {
                if (null != sendType) {
                    page = feedBackDao.pageWithNickNameAndSendType(nickName, sendType, pageable);
                } else {
                    page = feedBackDao.pageOnlyWithNickName(nickName, pageable);
                }
            }
        } else {
            if (StringUtils.isNotBlank(mobile)) {
                if (null != sendType) {
                    page = feedBackDao.pageWithMobileAndSendType(mobile, sendType, pageable);
                } else {
                    page = feedBackDao.pageOnlyWithMobile(mobile, pageable);
                }
            } else {
                if (null != sendType) {
                    page = feedBackDao.pageOnlyWithSendType(sendType, pageable);
                } else {
                    page = feedBackDao.findAll(pageable);
                }
            }
        }

        for (FeedBack feedBack : page.getContent()) {
            if (feedBack.getSendType().equals(Constant.COMPANY_USER_TYPE_PERSONAL)) {
                feedBack.setNickName(appUserDao.findOne(feedBack.getSendUserId()).getNickName());
            } else {
                feedBack.setNickName(companyUserDao.findOne(feedBack.getSendUserId()).getNickName());
            }
        }

        return page;
    }

    @Override
    @Transactional
    public Integer batchDelete(String ids) {
        try {
            Integer[] feedBackIds = JsonUtil.json2Obj(ids, Integer[].class);

            for (Integer feedBackId : feedBackIds) {
                feedBackDao.delete(feedBackId);
            }

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}